package palmtop.com.palmtop.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;



import org.apache.commons.codec.binary.Base64;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;

import palmtop.com.palmtop.R;
import palmtop.com.palmtop.activity.ImageDetailActivity;

/**
 * Created by Administrator on 2017/8/21.
 */
public final class FuncUtil {
    //private SharedPreferences sharedPreferences;
    Context context;


/*    public static void clickShowBigPic(final String picpath, View view, final Context context){
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(picpath!=null){
                    Intent i = new Intent(context,ImageDetailActivity.class);
                    i.putExtra("picpath", picpath);
                    context.startActivity(i);
                }else{
                    Toast.makeText(context, "未获取图片地址", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }*/

    public static void longClickShowBigpic(final String picpath, View view, final Context context){
        view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if(picpath!=null){
                    Intent i = new Intent(context,ImageDetailActivity.class);
                    i.putExtra("picpath", picpath);
                    context.startActivity(i);
                }else{
                    Toast.makeText(context, "未获取图片地址", Toast.LENGTH_SHORT).show();
                }
                return true;
            }
        });
    }


    public static void clickShowBigPic(final String picpath, View view, final Context context){
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(picpath!=null){
                    Intent i = new Intent(context,ImageDetailActivity.class);
                    i.putExtra("picpath", picpath);
                    context.startActivity(i);
                }else{
                    Toast.makeText(context, "未获取图片地址", Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        });
    }

    //获取网络图片
    public static Bitmap getHttpBitmap(String url){
        URL myFileURL;
        Bitmap bitmap=null;
        try{
            myFileURL = new URL(url);
            //获得连接
            HttpURLConnection conn=(HttpURLConnection)myFileURL.openConnection();
            //设置超时时间为6000毫秒，conn.setConnectionTiem(0);表示没有时间限制
            conn.setConnectTimeout(6000);
            //连接设置获得数据流
            conn.setDoInput(true);
            //不使用缓存
            conn.setUseCaches(false);
            //这句可有可无，没有影响
            //conn.connect();
            //得到数据流
            InputStream is = conn.getInputStream();
            //解析得到图片
            bitmap = BitmapFactory.decodeStream(is);
            //关闭数据流
            is.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return bitmap;

}

    public static boolean isLeader(String ismaster){
        if(ismaster.equals("是")){
            return true;
        }else{
            return false;
        }
    }

    public static<T> ArrayList<T> createArrayList(T ... elements){
        ArrayList<T> list = new ArrayList<T>();
        for (T element : elements) {
            list.add(element);
        }
        return list;
    }


    public static void updateno(String userid){
        RequestParams params = new RequestParams(Config.SERVER_URL + "taskapp/updateNo");
        params.addBodyParameter("userid", userid);
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });
    }

    /*public static void updateReadTask(String taskid){
        RequestParams params = new RequestParams(Config.SERVER_URL + "taskapp/updateReadTask");
        params.addBodyParameter("id", taskid);
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });
    }*/


    public static void updateReadClue(String id){
        RequestParams params = new RequestParams(Config.SERVER_URL + "clue/updateReadClue");
        params.addBodyParameter("id", id);
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });
    }

    public static void updateReadPopulation(String id){
        RequestParams params = new RequestParams(Config.SERVER_URL + "reportpopulation/updateReadPopulation");
        params.addBodyParameter("id", id);
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });
    }

    public static void updateReadTask(String taskid,String ssgrid,String puserid){
        RequestParams params = new RequestParams(Config.SERVER_URL + "readtask/updateReadTask");
        params.addBodyParameter("taskid", taskid);
        params.addBodyParameter("ssgrid", ssgrid);
        params.addBodyParameter("puserid", puserid);
        NetUtil.postData(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }
            @Override
            public void onFinished() {
            }
        });
    }





}
